package com.liuy.util.shell;

import java.io.BufferedInputStream;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 执行相应的命令
 * 
 * @deprecated 	现已废弃 
 * */
@Deprecated
public class CommandLine {
	
    public static void main(String[] args) throws IOException {
//        if (args == null || args.length == 0) {
//            System.out.println("请输入命令行参数(例：java CommandLine 'netstat')");
//        } else {
            String cmd = "";
            for (int i = 0; i < args.length; i++)
                cmd += " " + args[i];
            try {
                Runtime run = Runtime.getRuntime();
                //Process p = run.exec(cmd);//启动另一个进程来执行命令
                Process p = run.exec("ping  8.8.8.8");
                BufferedInputStream in = new BufferedInputStream(p.getInputStream());
                BufferedInputStream err = new BufferedInputStream(p.getErrorStream());
                BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
                BufferedReader errBr = new BufferedReader(new InputStreamReader(err));
                String lineStr;
                while ((lineStr = errBr.readLine()) != null)
                    System.out.println(lineStr);
                while ((lineStr = inBr.readLine()) != null)
                    System.out.println(lineStr);
                //检查命令是否执行失败。
                try {
                    if (p.waitFor()!=0) {
                        if(p.exitValue()==1)//p.exitValue()==0表示正常结束，1：非正常结束
                            System.err.println("命令执行失败!");
                    }
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
//    }

}
